﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class THAMSODAO
    {
        public DataTable LoadThamSo()
        {
            DataTable dtTS = new DataTable();
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_BangTS", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dtTS);
            cn.Close();
            return dtTS;
        }

        public void SuaThamSo(int ma, int giatri, string kieu)
        {
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_SuaTS", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@ma", SqlDbType.Int);
            cmd.Parameters.Add("@giatri", SqlDbType.Int);
            cmd.Parameters.Add("@kieu", SqlDbType.VarChar);
            //gán giá trị cho tham số
            cmd.Parameters["@ma"].Value = ma;
            cmd.Parameters["@giatri"].Value = giatri;
            cmd.Parameters["@kieu"].Value = kieu;
            //thực thi câu truy vấn
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        public DataTable LoadKhuyenMai()
        {
            DataTable dtKM = new DataTable();
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_BangKM", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dtKM);
            cn.Close();
            return dtKM;
        }

        public void ThemKhuyenMai(THAMSODTO tsDTO)
        {
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_ThemKM", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@tenkm", SqlDbType.VarChar);
            cmd.Parameters.Add("@giatri", SqlDbType.Int);
            cmd.Parameters.Add("@kieu",SqlDbType.VarChar);
            cmd.Parameters.Add("@ngaybd", SqlDbType.DateTime);
            cmd.Parameters.Add("@ngaykt", SqlDbType.DateTime);
            //gán giá trị cho tham số
            cmd.Parameters["@tenkm"].Value = tsDTO.TenTS;
            cmd.Parameters["@giatri"].Value = tsDTO.GiaTri;
            cmd.Parameters["@kieu"].Value = tsDTO.Kieu;
            cmd.Parameters["@ngaybd"].Value = tsDTO.ThoiGianBD;
            cmd.Parameters["@ngaykt"].Value = tsDTO.ThoiGianKT;
            //thực thi câu truy vấn
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        public void XoaKhuyenMai(int ma)
        {
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_XoaKM", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ma", SqlDbType.Int);
            cmd.Parameters["@ma"].Value = ma;
            cmd.ExecuteNonQuery();
            cn.Close();
        }

        public void SuaKhuyenMai(THAMSODTO tsDto)
        {
            SqlConnection cn = Connection.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_SuaKM", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@ma", SqlDbType.Int);
            cmd.Parameters.Add("@tenkm", SqlDbType.VarChar);
            cmd.Parameters.Add("@giatri", SqlDbType.Int);
            cmd.Parameters.Add("@kieu", SqlDbType.VarChar);
            cmd.Parameters.Add("@ngaybd", SqlDbType.DateTime);
            cmd.Parameters.Add("@ngaykt", SqlDbType.DateTime);
            //gán giá trị cho tham số
            cmd.Parameters["@ma"].Value = tsDto.MaTS;
            cmd.Parameters["@tenkm"].Value = tsDto.TenTS;
            cmd.Parameters["@giatri"].Value = tsDto.GiaTri;
            cmd.Parameters["@kieu"].Value = tsDto.Kieu;
            cmd.Parameters["@ngaybd"].Value = tsDto.ThoiGianBD;
            cmd.Parameters["@ngaykt"].Value = tsDto.ThoiGianKT;
            //thực thi câu truy vấn
            cmd.ExecuteNonQuery();
            cn.Close();
        }
    }
}
